<template>
  <view class="setting-container">
    <!-- 页面头部 -->
    <view class="page-header">
      <view class="header-content">
        <text class="page-title">设置中心</text>
        <text class="page-subtitle">个性化配置您的应用体验</text>
      </view>
    </view>

    <!-- 设置菜单区域 -->
    <view class="settings-section">
      <view class="section-title">
        <wd-icon name="setting" color="#667eea" size="24rpx" />
        <text class="title-text">账户设置</text>
      </view>

      <view class="menu-card">
        <view
          class="menu-item"
          @click="handleToPwd"
          :class="{ 'menu-item-active': activeItem === 'pwd' }"
          @touchstart="setActiveItem('pwd')"
          @touchend="clearActiveItem"
        >
          <view class="menu-item-left">
            <view class="icon-wrapper password-icon">
              <wd-icon name="lock" color="#4A90E2" size="32rpx" />
            </view>
            <view class="menu-text">
              <text class="menu-title">修改密码</text>
              <text class="menu-desc">更改您的登录密码</text>
            </view>
          </view>
          <view class="menu-arrow">
            <wd-icon name="arrow-right" color="#C0C4CC" size="24rpx" />
          </view>
        </view>
      </view>
    </view>

    <!-- 应用设置区域 -->
    <view class="settings-section">
      <view class="section-title">
        <wd-icon name="apps" color="#667eea" size="24rpx" />
        <text class="title-text">应用设置</text>
      </view>

      <view class="menu-card">
        <view
          class="menu-item"
          @click="handleToUpgrade"
          :class="{ 'menu-item-active': activeItem === 'upgrade' }"
          @touchstart="setActiveItem('upgrade')"
          @touchend="clearActiveItem"
        >
          <view class="menu-item-left">
            <view class="icon-wrapper upgrade-icon">
              <wd-icon name="refresh" color="#50C878" size="32rpx" />
            </view>
            <view class="menu-text">
              <text class="menu-title">检查更新</text>
              <text class="menu-desc">获取最新版本功能</text>
            </view>
          </view>
          <view class="menu-arrow">
            <wd-icon name="arrow-right" color="#C0C4CC" size="24rpx" />
          </view>
        </view>

        <view class="menu-divider"></view>

        <view
          class="menu-item"
          @click="handleCleanTmp"
          :class="{ 'menu-item-active': activeItem === 'clean' }"
          @touchstart="setActiveItem('clean')"
          @touchend="clearActiveItem"
        >
          <view class="menu-item-left">
            <view class="icon-wrapper clean-icon">
              <wd-icon name="delete" color="#FF6B6B" size="32rpx" />
            </view>
            <view class="menu-text">
              <text class="menu-title">清理缓存</text>
              <text class="menu-desc">释放存储空间</text>
            </view>
          </view>
          <view class="menu-arrow">
            <wd-icon name="arrow-right" color="#C0C4CC" size="24rpx" />
          </view>
        </view>
      </view>
    </view>

    <!-- 退出登录按钮 -->
    <view class="logout-section">
      <view class="logout-button" @click="handleLogout">
        <wd-icon name="logout" color="#ffffff" size="28rpx" />
        <text class="logout-text">退出登录</text>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useUserStore } from '@/store/modules/user'

const userStore = useUserStore()
const activeItem = ref<string | null>(null)

// 设置激活项
const setActiveItem = (item: string) => {
  activeItem.value = item
}

// 清除激活项
const clearActiveItem = () => {
  activeItem.value = null
}

// 修改密码
const handleToPwd = () => {
  uni.navigateTo({
    url: '/pages/mine/pwd/index'
  })
}

// 检查更新
const handleToUpgrade = () => {
  uni.showToast({
    title: '已经是最新版本',
    icon: 'success',
    duration: 2000
  })
}

// 清理缓存
const handleCleanTmp = () => {
  uni.showLoading({
    title: '清理中...'
  })

  setTimeout(() => {
    uni.hideLoading()
    uni.showToast({
      title: '清理缓存成功',
      icon: 'success',
      duration: 2000
    })
  }, 1500)
}

// 退出登录
const handleLogout = () => {
  uni.showModal({
    title: '提示',
    content: '确定注销并退出系统吗？',
    success: (res) => {
      if (res.confirm) {
        userStore.logout().then(() => {
          uni.reLaunch({
            url: '/pages/index'
          })
        }).catch(() => {
          uni.showToast({
            title: '退出失败',
            icon: 'error'
          })
        })
      }
    }
  })
}
</script>

<style lang="scss" scoped>
.setting-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 0;
  position: relative;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: radial-gradient(circle at 30% 20%, rgba(255,255,255,0.1) 0%, transparent 50%),
                radial-gradient(circle at 70% 80%, rgba(255,255,255,0.08) 0%, transparent 50%);
    pointer-events: none;
  }
}

/* 页面头部 */
.page-header {
  padding: 80rpx 40rpx 60rpx;
  text-align: center;
  position: relative;
  z-index: 2;

  .header-content {
    .page-title {
      display: block;
      font-size: 48rpx;
      font-weight: 700;
      color: #ffffff;
      margin-bottom: 16rpx;
      text-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.3);
      letter-spacing: 2rpx;
    }

    .page-subtitle {
      display: block;
      font-size: 28rpx;
      color: rgba(255, 255, 255, 0.8);
      font-weight: 400;
    }
  }
}

/* 设置区域 */
.settings-section {
  margin: 0 30rpx 40rpx;
  position: relative;
  z-index: 2;

  .section-title {
    display: flex;
    align-items: center;
    margin-bottom: 24rpx;
    padding: 0 20rpx;

    .title-text {
      font-size: 32rpx;
      font-weight: 600;
      color: #ffffff;
      margin-left: 12rpx;
      text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.3);
    }
  }
}

/* 菜单卡片 */
.menu-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20rpx);
  border-radius: 24rpx;
  overflow: hidden;
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.15);
  border: 1rpx solid rgba(255, 255, 255, 0.3);
}

/* 菜单项 */
.menu-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 32rpx 30rpx;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  cursor: pointer;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(102, 126, 234, 0.1), transparent);
    transition: left 0.5s;
  }

  &:active,
  &.menu-item-active {
    background: rgba(102, 126, 234, 0.08);
    transform: scale(0.98);

    &::before {
      left: 100%;
    }
  }

  &:not(:last-child) {
    border-bottom: 1rpx solid rgba(0, 0, 0, 0.06);
  }
}

.menu-item-left {
  display: flex;
  align-items: center;
  flex: 1;
}

/* 图标包装器 */
.icon-wrapper {
  width: 80rpx;
  height: 80rpx;
  border-radius: 20rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 24rpx;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: inherit;
    opacity: 0.1;
    border-radius: inherit;
  }
}

.password-icon {
  background: linear-gradient(135deg, #4A90E2, #357ABD);
  box-shadow: 0 4rpx 12rpx rgba(74, 144, 226, 0.3);
}

.upgrade-icon {
  background: linear-gradient(135deg, #50C878, #3A9B5C);
  box-shadow: 0 4rpx 12rpx rgba(80, 200, 120, 0.3);
}

.clean-icon {
  background: linear-gradient(135deg, #FF6B6B, #E55555);
  box-shadow: 0 4rpx 12rpx rgba(255, 107, 107, 0.3);
}

/* 菜单文本 */
.menu-text {
  display: flex;
  flex-direction: column;

  .menu-title {
    font-size: 32rpx;
    font-weight: 600;
    color: #333333;
    line-height: 1.4;
    margin-bottom: 4rpx;
    user-select: text;
    -webkit-user-select: text;

    &::selection {
      background: rgba(102, 126, 234, 0.3);
      color: #333333;
    }

    &::-moz-selection {
      background: rgba(102, 126, 234, 0.3);
      color: #333333;
    }
  }

  .menu-desc {
    font-size: 24rpx;
    color: #999999;
    line-height: 1.3;
    user-select: text;
    -webkit-user-select: text;

    &::selection {
      background: rgba(153, 153, 153, 0.3);
      color: #999999;
    }

    &::-moz-selection {
      background: rgba(153, 153, 153, 0.3);
      color: #999999;
    }
  }
}

/* 箭头 */
.menu-arrow {
  margin-left: 20rpx;
  transition: transform 0.3s ease;
}

.menu-item:active .menu-arrow {
  transform: translateX(4rpx);
}

/* 分割线 */
.menu-divider {
  height: 1rpx;
  background: linear-gradient(90deg, transparent, rgba(0, 0, 0, 0.1), transparent);
  margin: 0 30rpx;
}

/* 退出登录区域 */
.logout-section {
  margin: 60rpx 30rpx 40rpx;
  position: relative;
  z-index: 2;
}

.logout-button {
  background: linear-gradient(135deg, #FF6B6B, #E55555);
  border-radius: 28rpx;
  padding: 32rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16rpx;
  box-shadow: 0 8rpx 24rpx rgba(255, 107, 107, 0.4);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  cursor: pointer;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
    transition: left 0.5s;
  }

  &:active {
    transform: translateY(2rpx) scale(0.98);
    box-shadow: 0 4rpx 16rpx rgba(255, 107, 107, 0.5);

    &::before {
      left: 100%;
    }
  }

  .logout-text {
    font-size: 32rpx;
    font-weight: 600;
    color: #ffffff;
    text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.2);
    user-select: text;
    -webkit-user-select: text;

    &::selection {
      background: rgba(255, 255, 255, 0.3);
      color: #ffffff;
    }

    &::-moz-selection {
      background: rgba(255, 255, 255, 0.3);
      color: #ffffff;
    }
  }
}

/* 响应式设计 */
@media screen and (max-width: 750rpx) {
  .page-header {
    padding: 60rpx 30rpx 40rpx;

    .page-title {
      font-size: 42rpx;
    }

    .page-subtitle {
      font-size: 26rpx;
    }
  }

  .settings-section {
    margin: 0 20rpx 30rpx;
  }

  .menu-item {
    padding: 28rpx 24rpx;
  }

  .icon-wrapper {
    width: 70rpx;
    height: 70rpx;
    border-radius: 16rpx;
    margin-right: 20rpx;
  }

  .menu-text .menu-title {
    font-size: 30rpx;
  }

  .menu-text .menu-desc {
    font-size: 22rpx;
  }

  .logout-section {
    margin: 40rpx 20rpx 30rpx;
  }

  .logout-button {
    padding: 28rpx;
    border-radius: 24rpx;

    .logout-text {
      font-size: 30rpx;
    }
  }
}

/* 动画效果 */
.settings-section {
  animation: slideInUp 0.6s ease-out;
}

.settings-section:nth-child(2) {
  animation-delay: 0.1s;
}

.settings-section:nth-child(3) {
  animation-delay: 0.2s;
}

.logout-section {
  animation: slideInUp 0.6s ease-out 0.3s both;
}

@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(30rpx);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 全局文本选择样式 */
::selection {
  background: rgba(102, 126, 234, 0.3);
  color: inherit;
}

::-moz-selection {
  background: rgba(102, 126, 234, 0.3);
  color: inherit;
}
</style>
